package edu.qd.learn.day07;

/**
 * 头插法
 *
 * @author 布衣
 */
public class LearnHeadInsert<E> implements MyList<E>, MyStack<E> {

    private class Node<E> {
        E data;
        Node<E> next;

        public Node(E val) {
            this.data = val;
        }
    }

    private Node<E> head;
    private Node<E> tail;

    /**
     * 线性表里面的添加方法
     * @param e1
     */
    public void add(E e1){
        if (head == null) {
            head = new Node<>(e1);
            tail = head;
        }
        tail.next = new Node<>(e1);
        tail = tail.next;
    }

    /**
     * 压栈
     * @param val
     */
    public void push(E val) {
        if (head == null) {
            head = new Node<>(val);
            tail = head;
        }
        Node<E> temp = new Node<>(val);
        temp.next = head;
        head = temp;
    }

    /**
     * 弹栈
     * @return
     */
    public E pop(){
        E data = head.data;
        head = head.next;
        return data;
    }
}
